<script lang="ts">
import {Vue, Component, Prop, Watch, Emit, Inject} from 'vue-property-decorator';
import {InfoPreviewProps, fieldProps} from "./InfoPreview";
import cloneDeep  from 'lodash/cloneDeep';
import EditInput from "./EditInput.vue";
import {BusinessType, BusinessTypeEnum} from "../../type";
@Component({name: "infoPreview",
  components: {EditInput}
})
export default class InfoPreview extends Vue implements InfoPreviewProps{
  @Prop({default: [{}]}) fieldsList!: Array<fieldProps>;
  focusIndex: null | number = null;
  inner_fieldsList:fieldProps[] = [];
  @Watch("fieldsList", {immediate: true, deep: true})
  handleFieldsList(fieldsList: Array<fieldProps>) {
    this.inner_fieldsList = cloneDeep(fieldsList);
  }
  @Inject('modeType') injectedModeType!: {mode: string};
  @Inject("businessType")
  providedBusinessType!:BusinessType
  // 监听字段值的更新
  @Watch("inner_fieldsList", {immediate: true, deep: true})
  handleInnerFieldsList(fieldsList: Array<fieldProps>) {
    fieldsList.forEach(item => {
      if(item.multiple && item.key === 'licenses'){
        item!.licenses = item.value;
      }
    })
    this.handleUpdate(this.providedBusinessType.businessType, fieldsList)
  }
  @Emit("update")
  handleUpdate(type:BusinessTypeEnum, data:fieldProps[]){
    return {
      type, data
    }
  }
}
</script>

<template>
<div class="cc-info-preview__wrapper">
  <slot name="header"></slot>
  <div v-for="(item, index) in inner_fieldsList" :key="index" :class="{'cc-info-preview__item__wrapper': true, 'cc-info-preview__item__wrapper__edit': item.isEdited}">
    <div class="cc-info-preview__item__name">{{item.name}}：</div>
    <template v-if="item.render === 'image'">
      <template v-if="item.value">
        <el-image :src="item.value" style="margin: 5px 0;width:130px;height:130px" :preview-src-list="[item.value]"></el-image>
      </template>
    </template>
    <template v-else>
        <div style="line-height: 28px;">
          <EditInput v-model="item.value"
            :editable="item.editable && injectedModeType.mode != 'preview'"
            v-if="item"
            :item="item"
            :render="item.render">
          </EditInput>
      </div>
    </template>
  </div>
</div>
</template>

<style scoped lang="scss">
.cc-info-preview__item__new{
  font-weight: 700;
  line-height: 20px;
  display: inline-block;
  float: left;
  text-align: left;
}
.cc-info-preview__item__new_info{
  float:right;
  right: 80px;
  position: relative;
  font-weight: 400;
  font-size: 10px;
  color: #909399;
  display: inline-block;
  height: 20px;
  line-height: 20px;
}

.cc-info-preview__item__info{
  float:right;
  right: 80px;
  position: relative;
  font-weight: 400;
  font-size: 10px;
  color: #909399;
  display: inline-block;
  height: 20px;
  line-height: 20px;
}
.cc-info-preview__item__old{
  color:#909399;
  display: inline-block;
  float: left;
  text-align: left;
  height: 20px;
  line-height: 20px;
}
.cc-info-preview__wrapper{
  flex: 1;
  height: 100%
}
.cc-info-preview__wrapper::-webkit-scrollbar{

}
.cc-info-preview__wrapper::-webkit-scrollbar-track{
  background: #fff;
}
.cc-info-preview__wrapper::-webkit-scrollbar-thumb{
  background: #C0C4CC;
  border-radius: 3px;
}
.cc-info-preview__item__value:focus-within {
  border: none;
  outline: none;
}
.cc-info-preview__item__button{
  float: right;
  right: 20px;
}
.cc-info-preview__item__wrapper{
  overflow: hidden;
  min-height: 28px;
  margin: 0  6px 0 6px;
  align-self: stretch;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  position: relative;
  align-items: flex-start;
}

.cc-info-preview__item__wrapper + .cc-info-preview__item__wrapper__edit{
  height: auto;
}
.cc-info-preview__item__value:focus{
  color: #409EFF;
}
.cc-info-preview__item__new_button{
  color: #409eff;
  float: right;
  right: 20px;
  position: relative;
}
.cc-info-preview__item__new_button:hover{
  cursor: pointer;
}
.cc-info-preview__item__button{
  position: absolute;
  top: 0;
  right: 0;
  height: 28px;
  color: #409eff;
  cursor: pointer;
  line-height: 28px;
  font-size: 12px;
}
.cc-info-preview__item__name{
  float: left;
  width: 90px;
  font-size: 12px;
  font-weight: 400;
  line-height: 28px;
  text-align: left;
}
.cc-info-preview__item__value{
  overflow: hidden;
  font-weight: 700;
  line-height: 28px;
  text-align: left;
}
</style>